/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JanelaInicial.java
 *
 * Created on Dec 13, 2011, 11:30:17 PM
 */
package gui.janelas;

import java.awt.Frame;
import main.Main;

/**
 *
 * @author arthur
 */
public class JanelaInicial extends javax.swing.JDialog {

    Frame pai;
    
    /** Creates new form JanelaInicial */
    public JanelaInicial(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        
        this.pai = parent;
        
        initComponents();
        
        int w = getSize().width;
        int h = getSize().height;
        int x = (parent.getSize().width+w);
        int y = (parent.getSize().height+h)/2;

        setLocation(x, y);
        
        this.botaoCriarJogo.setEnabled(false);
        this.botaoConectar.setEnabled(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        labelNome = new javax.swing.JLabel();
        campoNome = new javax.swing.JTextField();
        labelIP = new javax.swing.JLabel();
        campoIP = new javax.swing.JTextField();
        botaoCriarJogo = new javax.swing.JButton();
        botaoConectar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        getContentPane().setLayout(new java.awt.GridBagLayout());

        labelNome.setText("Nome");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 2, 5);
        getContentPane().add(labelNome, gridBagConstraints);

        campoNome.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                digitarCampoNome(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        getContentPane().add(campoNome, gridBagConstraints);

        labelIP.setText("IP");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 2, 5);
        getContentPane().add(labelIP, gridBagConstraints);

        campoIP.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                digitarCampoIP(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        getContentPane().add(campoIP, gridBagConstraints);

        botaoCriarJogo.setText("Criar Jogo");
        botaoCriarJogo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clicarBotaoCriarJogo(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.ipadx = 50;
        gridBagConstraints.ipady = 7;
        gridBagConstraints.insets = new java.awt.Insets(7, 0, 7, 7);
        getContentPane().add(botaoCriarJogo, gridBagConstraints);

        botaoConectar.setText("Conectar");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.ipadx = 50;
        gridBagConstraints.ipady = 7;
        gridBagConstraints.insets = new java.awt.Insets(7, 7, 7, 0);
        getContentPane().add(botaoConectar, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void digitarCampoIP(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_digitarCampoIP
        verificarCampos();
    }//GEN-LAST:event_digitarCampoIP

    private void digitarCampoNome(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_digitarCampoNome
        verificarCampos();
    }//GEN-LAST:event_digitarCampoNome

    private void clicarBotaoCriarJogo(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clicarBotaoCriarJogo
        this.dispose();
        pai.dispose();
        
        Main.jogo.iniciarJanelaPrincipal(campoNome.getText());
    }//GEN-LAST:event_clicarBotaoCriarJogo

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                JanelaInicial dialog = new JanelaInicial(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botaoConectar;
    private javax.swing.JButton botaoCriarJogo;
    private javax.swing.JTextField campoIP;
    private javax.swing.JTextField campoNome;
    private javax.swing.JLabel labelIP;
    private javax.swing.JLabel labelNome;
    // End of variables declaration//GEN-END:variables


    private void verificarCampos(){
        if(campoNome.getText().length() > 0){
            this.botaoCriarJogo.setEnabled(true);
            if(campoIP.getText().length() > 0){
                this.botaoConectar.setEnabled(true);
            } else {
                this.botaoConectar.setEnabled(false);
            }
        } else {
            this.botaoCriarJogo.setEnabled(false);
            this.botaoConectar.setEnabled(false);
        }
    }
}
